package com.salmon.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.http.HttpStatus;

@JsonInclude(JsonInclude.Include.NON_EMPTY) // 为null或者""的不进行序列化
public class ApiResponse {

    private int code;
    private String message;
    private Object data;

    public ApiResponse(int code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ApiResponse() {
        this.code = HttpStatus.OK.value();
        this.message = HttpStatus.OK.getReasonPhrase();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static ApiResponse ofMessage(int code, String message) {
        return new ApiResponse(code, message, null);
    }

    public static ApiResponse ofSuccess() {
        return new ApiResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
    }
    
    public static ApiResponse ofSuccess(Object data) {
        return new ApiResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), data);
    }

    public static ApiResponse ofStatus(Status status) {
        return new ApiResponse(status.getCode(), status.getStandardMessage(), null);
    }

    public static ApiResponse ofStatus(HttpStatus status) {
        return new ApiResponse(status.value(), status.getReasonPhrase(), null);
    }

    @JsonIgnore
    //使之不在json序列化结果当中
    public boolean isSuccess(){
        // 成功状态码 200 和http 保持一致
        return this.code == HttpStatus.OK.value();
    }

    public enum Status {
        ADD_BRAND_CATEGORY_ERROR(500, "添加品牌类别失败"),
        ADD_BRAND_ERROR(500, "添加品牌失败"),
        UPDATE_ERROR(500, "更新数据失败"),
        ADD_ERROR(500,"添加失败"),
        DELETE_ERROR(500,"删除失败"),

        CART_EMPTY(2000, "购物车为空"),
        GOODS_NOT_SALE(2001, "商品不是在线售卖状态"),
        CREATE_ORDER_ERROR(2005, "生成订单错误"),
        ORDER_NOT_EXIST(2006, "用户此订单不存在"),
        ORDER_UP_PAY(2007, "已付款,无法取消订单"),

        NOT_VALID_PARAM(40005, "Not valid Params"),
        NOT_SUPPORTED_OPERATION(40006, "Operation not supported"),
        NOT_LOGIN(50000, "Not Login"),

        NO_PRIMARY_KEY(30000, "主键不能为空"),
        NOT_FOUND(404, "没有查询到数据"),
        NOT_FOUND_SPEC_GROUP(404, "商品规格组信息不存在"),
        NOT_FOUND_SPEC_ITEM(404, "商品规格项[参数]不存在"),
        NO_SHOP_ID(300001, "店铺ID不能为空");

        private int code;
        private String standardMessage;

        Status(int code, String standardMessage) {
            this.code = code;
            this.standardMessage = standardMessage;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getStandardMessage() {
            return standardMessage;
        }

        public void setStandardMessage(String standardMessage) {
            this.standardMessage = standardMessage;
        }
    }
}
